package TestPaperSystem.utils;

import TestPaperSystem.entity.ExamLimit;
import TestPaperSystem.service.ExamLimitService;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

@Component
@Slf4j(topic = "c.ThreadPool")
public class ThreadPool {

        private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("demo-pool-%d").build();

        public static ExecutorService pool = new ThreadPoolExecutor(5, 7,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(15), namedThreadFactory, new MyRejectPolicy());



        public void execute(Runnable task){
                pool.execute(task);
        }

        static class MyRejectPolicy implements RejectedExecutionHandler{

                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        log.warn("执行拒绝策略");
                        throw new RejectedExecutionException("线程池已满");
                }
        }
}
